<?php

class yiqiu_index extends Base{
    function ac_set(){

		global $dsql,$cfg_basehost,$cfg_version,$cfg_soft_lang;
		$dopost = request("dopost","");
		if(empty($dopost))
		{
			$ver = $this->ac_ver();
			$login_row = $dsql->GetOne("SELECT * FROM yiqiu_login_set");
			$login_type = unserialize($login_row['login_type']);
			$login_infos = unserialize($login_row['login_infos']);
			$templet = YIQIUTEMPLETS."/admin/setting.htm";
			$this->SetVar('login_type',$login_type);
			$this->SetVar('login_infos',$login_infos);
			$this->SetVar('ver',$ver);
			$this->SetVar('cfg_basehost',$cfg_basehost);
			$this->SetVar('cfg_version',$cfg_version);
			$this->SetVar('cfg_soft_lang',$cfg_soft_lang);
			$this->LoadTemplate($templet);
			$this->Display();
		}else if($dopost == 'save'){
			$login_type = request('login_type','');
			if(in_array('qq',$login_type))
			{
				$login_infos['qq']['appid'] = request('qq_appid','');
				$login_infos['qq']['appkey'] = request('qq_appkey','');
				$login_infos['qq']['callback'] = request('qq_callback','');
				if(empty($login_infos['qq']['appid']) or empty($login_infos['qq']['appkey']) or empty($login_infos['qq']['callback']))
				{
					ShowMsg("您的QQ登录参数配置不完整，请填写完整",-1);
					exit;
				}
			}
			
			if(in_array('weixin',$login_type)){
				$login_infos['weixin']['appid'] = request('wexin_appid','');
				$login_infos['weixin']['appsecret'] = request('wexin_appsecret','');
				$login_infos['weixin']['callback'] = request('wexin_callback','');
				if(empty($login_infos['weixin']['appid']) or empty($login_infos['weixin']['appsecret']) or empty($login_infos['weixin']['callback']))
				{
					ShowMsg("您的微信登录参数配置不完整，请填写完整",-1);
					exit;
				}
			}
			
			if(in_array('weibo',$login_type)){
				$login_infos['weibo']['appkey'] = request('weibo_appkey','');
				$login_infos['weibo']['appsecret'] = request('weibo_appsecret','');
				$login_infos['weibo']['callback'] = request('weibo_callback','');
				if(empty($login_infos['weibo']['appkey']) or empty($login_infos['weibo']['appsecret']) or empty($login_infos['weibo']['callback']))
				{
					ShowMsg("您的微博登录参数配置不完整，请填写完整",-1);
					exit;
				}
			}
			
			$login_row = $dsql->GetOne("SELECT * FROM yiqiu_login_set");
			$login_type_str = serialize($login_type);
			$login_infos_str = serialize($login_infos);

			if(empty($login_row))
			{
				$sql = "INSERT INTO yiqiu_login_set(`login_type`,`login_infos`) VALUES('{$login_type_str}','{$login_infos_str}')";
			}else{
				$sql = "UPDATE yiqiu_login_set SET login_type='{$login_type_str}',login_infos='{$login_infos_str}'";
			}
			
			if($dsql->ExecuteNoneQuery($sql))
			{
				ShowMsg("保存配置成功","index.php?m=admin&a=set");
				exit;
			}else{
				ShowMsg("保存失败",-1);
				exit;
			}
		}
        
    }
	
	function ac_ajax()
	{
		$html = ajax();
		echo $html;
	}
	
	function ac_update()
	{
		global $cfg_soft_lang;
		$parmurl = "http://api.yiqiu.org";
		$ver = $this->ac_ver();
		$key = $this->ac_key();
		
		$httpdown = new DedeHttpDown();
		//echo $parmurl."/?ac=update&key=".$key."&url=".$_SERVER['HTTP_HOST']."&ver={$ver}";exit;
		$httpdown->OpenUrl($parmurl."/?ac=update&key=".$key."&url=".$_SERVER['HTTP_HOST']."&ver={$ver}");
		$html = $httpdown->GetHtml();
		//var_dump($html);exit;
		if($cfg_soft_lang == 'gb2312') $html = utf82gb($html);
		if(!empty($html))
		{
			if($html != "error")
			{
				ShowMsg("现在可以更新到{$html}版本<a href='index.php?m=admin&a=updatenow&ver={$html}'>马上更新</a>","javascript:;");
				exit;
			}else{
				ShowMsg("请购买正版授权 <a href='http://wpa.qq.com/msgrd?v=3&uin=3627256&site=qq&menu=yes'>联系客服</a>","javascript:;");
				exit;
			}
			
		}else{
			ShowMsg("已经是最新版本".$ver,"javascript:;");
			exit;
		}
		
	}
	
	function ac_updatenow()
	{
		global $cfg_soft_lang,$dsql;
		$ver = request('ver','');
		$parmurl = "http://api.yiqiu.org";
		//echo $parmurl."/data/update/{$ver}/{$cfg_soft_lang}/update.txt";exit;
		$html = fopen_url($parmurl."/data/update/{$ver}/{$cfg_soft_lang}/update.zip");
		$sql = fopen_url($parmurl."/data/update/{$ver}/{$cfg_soft_lang}/update.sql");
		update_sql($sql);
		file_put_contents(YIQIUDATA."/update.zip",$html);
		$zip = new zip;
		$result = $zip->ExtractAll(YIQIUDATA."/update.zip",DEDEROOT);
		if(is_file(YIQIUDATA."/update.zip")) unlink(YIQIUDATA."/update.zip"); 
		ShowMsg("更新成功{$ver}成功","index.php?m=admin&a=update");
		exit;
	}
	
	function ac_code()
	{
		global $cfg_soft_lang;
		$file_path = YIQIUDATA."/code.txt";
		$func_path = YIQIUINC."/function.php";

		if(is_file($file_path) && is_file($func_path))
		{
			$html = D();
		}else{
			unlink(YIQIUDATA."/code.txt");
			$key = request('key','');
			if(!empty($key))
			{
				$parmurl = "http://api.yiqiu.org";
				$httpdown = new DedeHttpDown();
				$httpdown->OpenUrl($parmurl."/?ac=code&key=".$key."&url=".$_SERVER['HTTP_HOST']);
				$html = $httpdown->GetHtml();
				if($cfg_soft_lang == 'gb2312') $html = utf82gb($html);
				
				if($html != 2)
				{
					$httpdown->OpenUrl($html."&key=".$key."&lang=".$cfg_soft_lang."&url=".$_SERVER['HTTP_HOST']);
					$result = $httpdown->GetHtml();
					if($cfg_soft_lang == 'gb2312')
					{
						$result = utf82gb($result);
					}
					file_put_contents($func_path,$result);
					file_put_contents(YIQIUDATA."/code.txt",$key);
					$html = 1;
				}
			}
			
		}
		
		echo $html;
		
	}
    
	function ac_ver()
	{
		$ver = file_get_contents(YIQIUDATA."/ver.txt");
		return $ver;
	}
	
	function ac_key()
	{
		$code = file_get_contents(YIQIUDATA."/code.txt");
		return $code;
	}
	
}